package com.wanlong.admin.controller.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.wanlong.common.result.PageResult;
import com.wanlong.common.result.VueResult;
import com.wanlong.rpc.service.SysMemberManagerService;
import com.wanlong.sys.model.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 万龙
 * @name 角色管理
 * @project_name wanlong
 * @package_name com.wanlong.admin.controller.system
 * @description
 * @date 2019/2/23
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)//跨域请求
@RequestMapping(value = "/admin/role")
public class RoleController {
    private static ObjectMapper MAPPER = new ObjectMapper();

    @Autowired
    public SysMemberManagerService sysMemberManagerService;

    @RequestMapping(value = "/roleList" , method = RequestMethod.GET)
    @ResponseBody
    public VueResult roleList(@RequestParam("name")String name, @RequestParam("page")Integer page, @RequestParam("limit")Integer limit){
        PageInfo<SysRole> sysRoleList = sysMemberManagerService.getPageSysRoleListByName(name,page,limit);
        List<SysRole> ul = sysRoleList.getList();
        PageResult pageResult = new PageResult();
        pageResult.setItems(ul);
        pageResult.setTotal(sysRoleList.getTotal());

//        Map<String ,Object> map = new HashMap<>();
//        map.put("items",ul);
//        map.put("total",sysRoleList.getTotal());
//        String result="";
//        try {
//            result= MAPPER.writeValueAsString(map);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
        return VueResult.success(pageResult);
    }

    @RequestMapping(value = "/createRole" , method = RequestMethod.POST)
    @ResponseBody
    public VueResult createRole(@RequestBody SysRole sysRole){

        String result=sysMemberManagerService.insertSysRole(sysRole)+"";
        return VueResult.success(result);
    }

    @RequestMapping(value = "/updateRole" , method = RequestMethod.POST)
    @ResponseBody
    public VueResult updateRole(@RequestBody SysRole sysRole){
        sysRole.setGmtModified(new Date());
        String result=sysMemberManagerService.updateSysRole(sysRole)+"";

        return VueResult.success();
    }

    @RequestMapping(value = "/deleteRole" , method = RequestMethod.POST)
    @ResponseBody
    public VueResult deleteRole(@RequestBody SysRole sysRole){
        sysRole.setGmtModified(new Date());

        String result=sysMemberManagerService.deleteSysRole(sysRole)+"";

        return VueResult.success();
    }

    @RequestMapping(value = "/getAllRoleList" , method = RequestMethod.GET)
    @ResponseBody
    public VueResult getAllRoleList(){
        List<SysRole> sysRoleList = sysMemberManagerService.getAllSysRoleList();
//        String result="";
//        try {
//            result= MAPPER.writeValueAsString(sysRoleList);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
        return VueResult.success(sysRoleList);
    }

}
